package com.auemall.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.auemall.constant.ApplicationConstant;
import com.auemall.po.Attribute;
import com.auemall.po.AttributeValues;
import com.auemall.po.ProductAttribute;
import com.auemall.po.ProductBrand;
import com.auemall.po.ProductLadderPrice;
import com.auemall.po.ProductSku;
import com.auemall.po.Productinfo;
import com.auemall.service.AttributeService;
import com.auemall.service.AttributeValuesService;
import com.auemall.service.ProductAttributeService;
import com.auemall.service.ProductBrandService;
import com.auemall.service.ProductCategoryService;
import com.auemall.service.ProductInfoService;
import com.auemall.service.ProductLadderPriceService;
import com.auemall.service.ProductSkuService;
import com.auemall.vo.AttributesAndValues;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class ProductController {
	
	@Autowired
	private ProductCategoryService productCategoryService;
	
	@Autowired
	private AttributeService attributeService;
	
	@Autowired
	private AttributeValuesService attributeValuesService;
	
	
	@Autowired
	private ProductInfoService productInfoService;
	
	@Autowired
	private ProductAttributeService productAttributeService;
	
	
	@Autowired
	private ProductBrandService productBrandService;
	
	@Autowired
	private ProductSkuService productSkuService;
	
	@Autowired
	private ProductLadderPriceService productLadderPriceService;
	
	
	//显示所有的品牌信息
	@RequestMapping("/product/list")
	public String showProduct() {
		
		return "product/productList";
	}
	
	
	//显示所有的品牌信息
	@SuppressWarnings("unchecked")
	@RequestMapping("/product/search")
	public String productSearch(HttpServletRequest request,Integer id,String values,Integer currentPage) {
		
		
		List<AttributesAndValues> attributes = null;
		
		attributes = attributeService.findAttributeByCatageryId(id,values,request,currentPage);

		request.setAttribute("attList",attributes);
		
		request.setAttribute("id", id);
		
		request.setAttribute("str",values);
		
		return "product/productSearch";
	}
	
	//商品详情
	@RequestMapping("/product/productDetail")
	public String productDetail(Integer pid,HttpServletRequest request) {
		
		Productinfo product = productInfoService.findProductInfoById(pid);
		
		
		ProductBrand brand = productBrandService.selectByPrimaryKey(product.getBrandId());
		
		request.setAttribute("product",product);
		
		request.setAttribute("brand",brand);
		
		//查询对应的规格
		
		List<ProductSku> list = this.productSkuService.findProductStuByPid(product.getId());
		
		if (list != null && list.size() >0) {
			
			ProductSku sku = list.get(0);
			
			request.setAttribute("productSku", sku);
			
			//查询阶梯价格
			List<ProductLadderPrice>  ladderList = productLadderPriceService.findLadderPriceByProSku(sku.getId());
			
			if (ladderList != null && ladderList.size() >0) {
				
				request.setAttribute("ladder", ladderList);
				
			}
			
		}
		
		if (pid == null) {
			
			return "error";
		}
		
		//查询对应的属性
		
		List<AttributeValues> values = this.productAttributeService.findAllAttributeByPid(pid);
		
		request.setAttribute("attrs",values);
		
		return "product/producDetail";
	}
	
	
	
}
